{% extends "technological/base.html" %}

{% block page_link %}
    <script src="../../static/technological/js/sponsor.js"></script>
    <link rel="stylesheet" href="../../static/technological/css/style.css">
{% endblock %}


{% block style %}
    <style>
        .add_class {
            background: #1E90FF;
        }
        .basic-tabs{
            box-shadow: 0 1.5px 0 0 #1E90FF;
            color: #1E90FF;
        }
        .basic{
            width: 8%;height: 40px;line-height: 40px;text-align: center;display: inline-block;vertical-align: top;cursor: pointer;
        }
        .input-style{
            display: inline-block;
            width: 73%;
            height: 35px;
            border-radius: 7px;
            font-size: 14px;
            padding-left: 1%;
            border: #cfcfcf solid 1px;
            margin-left: 1%;
        }

        .textarea-style{
            display: inline-block;
            width: 73%;
            border-radius: 7px;
            font-size: 14px;
            padding-left: 1%;
            border: #cfcfcf solid 1px;
            margin-left: 1%;
        }
        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }
        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
        .halving_line{
            width: 80%;
            border: #cfcfcf solid 0.5px;
            margin-left: 2%;
        }
        .label_tag{
            width: 100px;
            text-align: right;
            vertical-align: top;
            font-size: 14px;
            line-height: 35px;
        }
        .add-station{
            width: 60%;height: 35px;background: #0070c1;border-color: #c5dbec;color: #ffffff;
        }
        tr{
            height: 35px;
        }
        th{
            text-align: center;
        }
        table {
            table-layout: fixed;
        }
        td:first-child, th:first-child {
            position:sticky;
            left:0; /* 首行永远固定在左侧 */
            z-index:1;
        }
        thead tr th {
            position:sticky;
            top:0; /* 列首永远固定在头部  */
            background-color: #78C3F3;
            color: #00688B;
        }
        th:first-child{
            z-index:2;
        }
        .model_li{
            color: #3f414d;
            width: 25%;
            cursor: pointer;
            display: none;
        }
    </style>
{% endblock %}



{% block right_content %}
    <div class="headline">
        <div style="width: 15%;height: 100%;line-height: 40px;float: left;padding-left: 1%;">
            <a href="sponsor.html" style="color: #0b0b0b;">
                <i class="iconfont icon-faqi"></i>
                <span> 流程发起</span><span> > </span><span> 表单</span>
            </a>
        </div>

        <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button id="add_station" type="button" class="btn btn-default add-station preserve" onclick="summit_val()">提交</button>
        </div>
    </div>


    <div style="width: 100%;height: 40px;padding-left: 3%;box-shadow: 0 1px 0 0 #CFCFCF;">

        <div id="create-menu" class="basic basic-tabs" onclick="Control_navigation(this.id)">
            申请表单
        </div>

        <div id="create-flow" class="basic" onclick="Control_navigation(this.id)">
            流程图
        </div>
    </div>


    <!-- 流程图 -->
    <div id="flow" class="flow_chart" style="width: 100%;height: 88%;display: none;margin-top: 0.2%;">
        <div id="flow_chart"></div>
        <textarea name="" id="result" cols="30" style="display: none;"></textarea>
    </div>

    <!-- 表单 -->
    <div id="menu_from" style="width: 100%;height: 88%;overflow: auto;margin-top: 0.2%;">
        <h3 id="form_name" style="text-align: center;font-size: 20px;margin-top: 1%;">{{ form_name }}</h3>

        <div style="width: 95%;margin-left: 2.5%;margin-top: 1%;background: white;height: 40px;padding-left: 0.5%;line-height: 40px;">
            基本信息
        </div>

        <div style="border: #CFCFCF solid 0.5px;width: 95%;margin-left: 2.5%;margin-top: 1%;padding: 1%;">
            <div>
                <label for="" class="label_tag">申请人：</label>
                <input id="proposer" type="text" class="input-style" style="width: 32%;" value="{{ real_name }}" disabled>

                <label for="" class="label_tag">部门：</label>
                <input id="department" type="text" class="input-style" style="width: 32%;" value="{{ department }}" disabled>
            </div>

            <div style="margin-top: 1%;">
                <label for="" class="label_tag">申请时间：</label>
                <input id="now_time" type="text" class="input-style" style="width: 74.5%;" disabled>
            </div>
        </div>

        <div style="width: 95%;margin-left: 2.5%;margin-top: 1%;background: white;height: 40px;padding-left: 0.5%;line-height: 40px;">
            申请详情
        </div>

        <form id="menu_from_1" style="border: #CFCFCF solid 0.5px;width: 95%;margin-left: 2.5%;margin-top: 1%;">

        </form>

        <div style="width: 95%;margin-left: 2.5%;margin-top: 1%;background: white;height: 40px;padding-left: 0.5%;line-height: 40px;">
            <label>附件： </label>
            <a href="javascript:;" class="file" style="vertical-align:middle;">
                <i class="iconfont icon-upload"></i>
                上传附件
                <input type="file" name="accessory" id="accessory" onchange="upload_accessory()">
            </a>
        </div>

        <div style="border: #CFCFCF solid 0.5px;width: 95%;margin-left: 2.5%;height: 100px;margin-top: 1%;overflow: auto;">
            <div style="display: inline-block;vertical-align: top;width: 100%;">
                <table class="table table-hover table-bordered" style="border-collapse: collapse;width: 100%;">
                    <thead>
                        <tr>
                            <th scope="col" style="width: 3%;">
                                <input type="checkbox" onclick="check_all_not()" id="check_all">
                            </th>
                            <th scope="col" style="width: 8%;">序号</th>
                            <th scope="col" style="width: 30%;">文件名称</th>
                            <th scope="col" style="width: 8%;">文件大小</th>
                            <th scope="col" style="width: 15%;">上传时间</th>
                            <th scope="col" style="width: 5%;">操作</th>
                        </tr>
                    </thead>
                    <tbody style="overflow: auto;" id="t_table"></tbody>
                </table>
            </div>
        </div>


        <div style=";width: 95%;margin-left: 2.5%;height: 100px;margin-top: 1%;">

        </div>

    </div>


{% endblock %}

{% block script %}
    <script type="text/javascript" src="../../static/public_file/other/jquery.js"></script>
{#    <script type="text/javascript" src="../../static/public_file/GooFlow/data.js"></script>#}
    <script type="text/javascript" src="../../static/public_file/js/GooFunc.js"></script>
    <script type="text/javascript" src="../../static/public_file/js/json2.js"></script>
    <script type="text/javascript" src="../../static/public_file/GooFlow/codebase/GooFlow.js"></script>
    <script type="text/javascript">
        var property = {};
        var demo;
        var jsondata = {{ jsondata | safe }}
        $(document).ready(function() {
            demo = $.createGooFlow($("#flow_chart"), property);
            demo.loadData(jsondata);
            document.getElementsByClassName("GooFlow_work")[0].style.width = "100%";
        });


        var Minutes = new Date().getUTCMinutes();
        if (Minutes < 10){
            Minutes = '0' + String(Minutes)
        }

        var Seconds = new Date().getSeconds();
        if (Seconds < 10){
            Seconds = '0' + String(Seconds)
        }
        var now_date = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + " " + new Date().getHours() + ":" + Minutes + ":" + Seconds;
        document.getElementById("now_time").value = now_date;

        function fromToJson(form) {
            var result = {};
            //获取表单的数组对象
            var fieldArray = $('#' + form).serializeArray();
            //将表单转化为JSON对象
            for (var i = 0; i < fieldArray.length; i++) {
                var field = fieldArray[i];
                if (field.name in result) {
                    result[field.name] += ',' + field.value;
                } else {
                    result[field.name] = field.value;
                }
            }
            return result;
        }

        // 提交数据
        function summit_val() {
            document.getElementById("loading").style.display = "inline-block";
            var postData = fromToJson("menu_from_1");   // 获取form表单数据
            postData["proposer"] = document.getElementById("proposer").value;
            postData["department"] = document.getElementById("department").value;
            postData["create_time"] = document.getElementById("now_time").value;

            var tr_tag = document.getElementById("t_table");

            var file_list = new Array();
            for (var i=0; i<tr_tag.children.length; i++) {
                var file_dict = {};
                file_dict["file_name"] = tr_tag.children[i].children[2].innerHTML;
                file_dict["file_size"] = tr_tag.children[i].children[3].innerHTML;
                file_dict["up_time"] = tr_tag.children[i].children[4].innerHTML;
                file_dict["file_path"] = tr_tag.children[i].children[5].children[0].href;
                file_list.push(file_dict);
            }
            postData["file_list"] = file_list;
            postData["form_name"] = document.getElementById("form_name").innerText;
            postData["form_url"] = window.location.pathname + "flowId=" + window.location.href.split("=")[1];
            postData["form_id"] = window.location.href.split("=")[1];
            postData["diagram"] = JSON.stringify(jsondata);

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/technological/save_apply_msg", true);
            xhr.send(JSON.stringify(postData));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate['message'])
                        if (JsonDate["code"] === 200){
                            alert("提交成功")
                        }
                        else {
                            alert(JsonDate['message'])
                        }
                    }
                }
            };
        }

        var json_form = JSON.parse({{ form_data | safe }});

        var tag_name = "";
        var tag_type = "";
        var tag_placeholder = "";
        var label_name = "";
        var tag_id = "";
        var menu_from = document.getElementById("menu_from_1");


        for (var i=0; i<json_form["list"].length; i++) {
            if (json_form["list"][i]["compName"] === "栅格布局") {
                var div = document.createElement("div");
                div.style.padding = '1%';
                for (var t=0; t<json_form["list"][i]["columns"].length; t++) {
                    var div_tag = document.createElement("div");
                    div_tag.style.display = "inline-block";div_tag.style.width = "43%";
                    tag_name = json_form["list"][i]["columns"][t]["list"][0]["compIcon"];
                    tag_type = json_form["list"][i]["columns"][t]["list"][0]["viewType"];
                    tag_placeholder = json_form["list"][i]["columns"][t]["list"][0]["placeholder"];
                    tag_id = json_form["list"][i]["columns"][t]["list"][0]["id"];
                    label_name = json_form["list"][i]["columns"][t]["list"][0]["label"];

                    {#var tag_label = document.createElement(tag_name);#}
                    var label = document.createElement("label");
                    label.innerHTML = label_name;label.className = "label_tag";

                    create_tag(div_tag, tag_name, label);

                    div.appendChild(div_tag);
                    menu_from.appendChild(div)
                }
            }else {
                tag_name = json_form["list"][i]["compIcon"];
                tag_type = json_form["list"][i]["viewType"];
                tag_placeholder = json_form["list"][i]["placeholder"];
                tag_id = json_form["list"][i]["id"];
                label_name = json_form["list"][i]["label"];

                var div1 = document.createElement("div");
                div1.style.padding = '1%';

                var label1 = document.createElement("label");
                label1.innerHTML = label_name;label1.className = "label_tag";

                create_tag(div1, tag_name, label1);
            }
        }


        function create_tag(div1, tag_name, label1) {
            if (tag_name === "textarea"){
                textarea = create_textarea();
                textarea.className = "textarea-style";textarea.setAttribute("placeholder", tag_placeholder);
                div1.appendChild(label1);div1.appendChild(textarea);
                menu_from.appendChild(div1)
            }else if (tag_name === "att") {             // 多行文本框
                file = create_upfile();
                div1.appendChild(label1);div1.appendChild(file);
                {#div1.style.textAlign = "left";#}
                menu_from.appendChild(div1)
            }else if (tag_name === "input"){            // input标签
                var input_tag = create_input();
                input_tag.setAttribute("placeholder", tag_placeholder);
                div1.appendChild(label1);div1.appendChild(input_tag);
                menu_from.appendChild(div1);
            }else if (tag_name === "date"){         // 时间日期
                var date_tag = create_date();
                div1.appendChild(label1);div1.appendChild(date_tag);
                menu_from.appendChild(div1);
            }else if (tag_name === "divider"){          // 分割线
                var halving_line_tag = create_halving_line();
                menu_from.appendChild(halving_line_tag);
            }else if (tag_name === "select"){
                var select_tag = create_select();
                div1.appendChild(label1);div1.appendChild(select_tag);
                menu_from.appendChild(div1);
            }
        }

        
        function create_textarea() {
            var textarea = document.createElement("textarea"); //cols="30" rows="10"
            textarea.setAttribute("cols", "30");textarea.setAttribute("rows", "5");textarea.setAttribute("name", tag_id);textarea.setAttribute("id", tag_id);
            return textarea
        }

        function create_upfile() {
            var div = document.createElement("div");
            div.style.width = "80%";div.style.display = "inline-block";div.style.textAlign = "left";div.style.paddingLeft = "1%";
            var div1 = document.createElement("div");
            div1.innerHTML = '<a href="javascript:;" class="file"><i class="iconfont icon-upload"></i> 上传<input type="file" name="" id="signature_picture"></a>';

            div.appendChild(div1);
            return div
        }

        function create_input() {
            var input = document.createElement("input");
            input.className = "input-style";input.setAttribute("name", tag_id);input.setAttribute("id", tag_id);
            return input
        }
        
        function create_date() {
            var input = document.createElement("input");
            input.className = "input-style";input.setAttribute("type", "datetime-local");input.setAttribute("name", tag_id);input.setAttribute("id", tag_id);
            return input
        }

        function create_halving_line() {
            var div = document.createElement("div");
            div.className = "halving_line";
            return div
        }

        function create_select() {
            var vacation_list = ["事假", "病假", "婚假", "丧假", "产假", "探亲假", "年假", "公假"];
            var select = document.createElement("select");
            select.className = "input-style";select.setAttribute("name", tag_id);select.setAttribute("id", tag_id);
            for (var i=0; i<vacation_list.length; i++){
                var option = document.createElement("option");
                option.innerHTML = vacation_list[i];
                select.appendChild(option);
            }
            return select
        }
    </script>
{% endblock %}
